home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / math / random / frmrand.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1994-10-31  |  7.8 KB  |  250 lines

  1. VERSION 2.00
  2. Begin Form Random 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "Random Number Generator"
  5.    ClientHeight    =   4020
  6.    ClientLeft      =   1965
  7.    ClientTop       =   1620
  8.    ClientWidth     =   4590
  9.    Height          =   4425
  10.    Icon            =   FRMRAND.FRX:0000
  11.    Left            =   1905
  12.    LinkTopic       =   "Form1"
  13.    ScaleHeight     =   4020
  14.    ScaleWidth      =   4590
  15.    Top             =   1275
  16.    Width           =   4710
  17.    Begin TextBox txtValue 
  18.       Height          =   285
  19.       Left            =   2580
  20.       TabIndex        =   1
  21.       Top             =   660
  22.       Width           =   1095
  23.    End
  24.    Begin TextBox txtMin 
  25.       Height          =   285
  26.       Left            =   2580
  27.       TabIndex        =   2
  28.       Top             =   1860
  29.       Width           =   1095
  30.    End
  31.    Begin TextBox txtMax 
  32.       Height          =   285
  33.       Left            =   2580
  34.       TabIndex        =   3
  35.       Top             =   2460
  36.       Width           =   1095
  37.    End
  38.    Begin CommandButton cmdGenerate 
  39.       Caption         =   "&Generate"
  40.       Height          =   375
  41.       Left            =   1680
  42.       TabIndex        =   4
  43.       Top             =   3360
  44.       Width           =   1335
  45.    End
  46.    Begin Shape Shape1 
  47.       Height          =   2775
  48.       Left            =   480
  49.       Top             =   300
  50.       Width           =   3615
  51.    End
  52.    Begin Label Label1 
  53.       BackColor       =   &H00C0C0C0&
  54.       Caption         =   "Number of Values"
  55.       Height          =   375
  56.       Left            =   900
  57.       TabIndex        =   0
  58.       Top             =   660
  59.       Width           =   1335
  60.    End
  61.    Begin Label Label2 
  62.       BackColor       =   &H00C0C0C0&
  63.       Caption         =   "Minimum Value"
  64.       Height          =   375
  65.       Left            =   900
  66.       TabIndex        =   5
  67.       Top             =   1860
  68.       Width           =   1335
  69.    End
  70.    Begin Label Label3 
  71.       BackColor       =   &H00C0C0C0&
  72.       Caption         =   "Maximum Value"
  73.       Height          =   375
  74.       Left            =   900
  75.       TabIndex        =   6
  76.       Top             =   2460
  77.       Width           =   1335
  78.    End
  79. Option Explicit
  80.     Dim sMsg As String
  81. Sub cmdGenerate_Click ()
  82.     'Install error handler
  83.     On Error GoTo UnexpectedOops
  84.     'Test for valid range
  85.     If Val(TxtMax.Text) <= Val(TxtValue.Text) + Val(TxtMin.Text) Then
  86.         TxtMax.SetFocus
  87.         sMsg = "Range must be larger than the number of values generated."
  88.         MsgBox sMsg, 64, "Error"
  89.         sMsg = ""
  90.         Exit Sub
  91.     End If
  92.     ReDim numbers(1 To TxtValue.Text) As Integer
  93.     Dim I As Integer, n As Integer, temp As Integer
  94.     Randomize       ' seed random number generator
  95.     I = 1
  96.     Do
  97.                     ' generate random number between Min and Max
  98.         temp = Int(Rnd(1) * ((TxtMax.Text - TxtMin.Text) + 1) + TxtMin.Text)
  99.         If I = 1 Then  ' don't test if first number (will be = to itself)
  100.             numbers(I) = temp
  101.             I = I + 1
  102.         Else
  103.             For n = 1 To I - 1
  104.                 If numbers(n) = temp Then   ' check all numbers for duplicates
  105.                     Exit For
  106.                 End If
  107.             Next n
  108.             If numbers(n) <> temp Then      ' temp is unique
  109.                 numbers(I) = temp
  110.                 I = I + 1                   ' advance counter
  111.             Else
  112.                 ' do nothing, don't save temp to numbers() and
  113.                 ' don't advance I.
  114.                 ' go through loop again to search for a unique number
  115.             End If
  116.         End If
  117. Loop While I <= TxtValue.Text       ' repeat until you have enough unique numbers
  118.     ' Generate message box to display numbers
  119. For I = 1 To UBound(numbers)
  120.     sMsg = sMsg + Str$(numbers(I)) & ", "
  121. Next I
  122. MsgBox sMsg, 64, "Unique Random Numbers"
  123. sMsg = ""
  124. Exit Sub
  125. UnexpectedOops:
  126.     MsgBox Error$(Err)
  127.     Exit Sub
  128. End Sub
  129. Sub DrawFrame (TargetControl As Control, FrameWidth, FrameStyle)
  130. ' Function: Draw a 3D outline around a control.
  131. ' Syntax: DrawFrame Control, Width, Style
  132. ' Control = name of control the outline should
  133. '           be drawn around
  134. ' Width   = width of the outline
  135. ' Style   = Raised or Sunken look
  136. '           0 = Raised
  137. '           1 = Sunken
  138. ' Example: DrawFrame Text1, 2, 1
  139. ' gives a sunken 3D look to text1
  140.     Dim lft%, Rite%, Btm%, Tp%
  141.     Dim LftLine%, BtmLine%
  142.         'Determine style of outline
  143.     Select Case FrameStyle
  144.         Case 0                  'Raised
  145.             LftLine = 15
  146.             BtmLine = 0
  147.         Case 1                  'Sunken
  148.             LftLine = 0
  149.             BtmLine = 15
  150.     End Select
  151.         'Calculate coordinates of outline
  152.     lft = TargetControl.Left
  153.     Rite = TargetControl.Left + TargetControl.Width
  154.     Tp = TargetControl.Top
  155.     Btm = TargetControl.Top + TargetControl.Height
  156.     TargetControl.Parent.DrawWidth = FrameWidth
  157.         
  158.         'Draw Top line
  159.     TargetControl.Parent.Line (lft, Tp)-(Rite, Tp), QBColor(LftLine)
  160.         'Draw Left line
  161.     TargetControl.Parent.Line (lft, Tp)-(lft, Btm), QBColor(LftLine)
  162.         'Draw Bottom line
  163.     TargetControl.Parent.Line (lft, Btm)-(Rite, Btm), QBColor(BtmLine)
  164.         'Draw Right Line
  165.     TargetControl.Parent.Line (Rite, Tp)-(Rite, Btm), QBColor(BtmLine)
  166. End Sub
  167. Sub Form_Paint ()
  168.     DrawFrame TxtValue, 2, 1
  169.     DrawFrame TxtMin, 2, 1
  170.     DrawFrame TxtMax, 2, 1
  171. End Sub
  172. Sub Form_Unload (Cancel As Integer)
  173.     About.Show
  174. End Sub
  175. Sub txtMax_KeyPress (keyascii As Integer)
  176.     If keyascii < Asc("0") Or keyascii > Asc("9") Then
  177.         keyascii = 0            ' cancel the character
  178.         Beep                    ' sound error signal
  179.     End If
  180. End Sub
  181. Sub txtMax_LostFocus ()
  182.     If TxtMax.Text = "" Then
  183.         TxtMax.SetFocus
  184.         sMsg = "Please enter a Maximum value."
  185.         MsgBox sMsg, 64, "Error"
  186.         sMsg = ""
  187.         Exit Sub
  188.     End If
  189.     If Val(TxtMax.Text) <= Val(TxtMin.Text) Then
  190.         TxtMax.SetFocus
  191.         sMsg = "Maximum value must be greater than minimum value."
  192.         MsgBox sMsg, 64, "Error"
  193.         sMsg = ""
  194.         Exit Sub
  195.     End If
  196.     If Val(TxtMax.Text) <= Val(TxtValue.Text) + Val(TxtMin.Text) Then
  197.         TxtMax.SetFocus
  198.         sMsg = "Range must be larger than the number of values generated."
  199.         MsgBox sMsg, 64, "Error"
  200.         sMsg = ""
  201.         Exit Sub
  202.     End If
  203.     If Val(TxtMax.Text) >= 32767 Then
  204.         sMsg = "Number must be less than 32,767."
  205.         Beep
  206.         MsgBox sMsg, 64, "Error"
  207.         TxtMax.SetFocus
  208.         sMsg = ""
  209.         Exit Sub
  210.     End If
  211. End Sub
  212. Sub txtMin_KeyPress (keyascii As Integer)
  213.     If keyascii < Asc("0") Or keyascii > Asc("9") Then
  214.         keyascii = 0            ' cancel the character
  215.         Beep                    ' sound error signal
  216.     End If
  217. End Sub
  218. Sub txtMin_LostFocus ()
  219.     If TxtMin.Text = "" Then
  220.         TxtMin.SetFocus
  221.         sMsg = "Please enter a Minimum value."
  222.         MsgBox sMsg, 64, "Error"
  223.         sMsg = ""
  224.         Exit Sub
  225.     End If
  226.     If Val(TxtMin.Text) >= 32767 Then
  227.         sMsg = "Number must be less than 32,767."
  228.         Beep
  229.         MsgBox sMsg, 64, "Error"
  230.         TxtMin.SetFocus
  231.         sMsg = ""
  232.         Exit Sub
  233.     End If
  234. End Sub
  235. Sub txtValue_KeyPress (keyascii As Integer)
  236.     If keyascii < Asc("0") Or keyascii > Asc("9") Then
  237.         keyascii = 0            ' cancel the character
  238.         Beep                    ' sound error signal
  239.     End If
  240. End Sub
  241. Sub txtValue_LostFocus ()
  242.     If TxtValue.Text = "" Then
  243.         TxtValue.SetFocus
  244.         sMsg = "Please enter a number of values to generate."
  245.         MsgBox sMsg, 64, "Error"
  246.         sMsg = ""
  247.         Exit Sub
  248.     End If
  249. End Sub
  250.